package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseOnNext;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.task.BindPhoneTask;
import com.zw.yzk.kabu.data.network.task.GetVerifyCodeTask;
import com.zw.yzk.kabu.data.network.task.RegisterTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.view.view.BindPhoneView;
import com.zw.yzk.kabu.view.view.RegisterView;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.inject.Named;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;

/**
 * Created by zhanwei on 2016/12/24.
 */

public class BindPhonePresenter implements Presenter {

    private final static long VERIFY_INTERVAL = 60;

    private UseCase bind;
    private UseCase getVerifyCode;
    private BindPhoneView bindView;
    private AppCompatActivity activity;
    private Subscription showCountDown;

    @Inject
    public BindPhonePresenter(@Named("bindPhoneUseCase") UseCase bind,
                              @Named("verifyCodeUseCase") UseCase getVerifyCode,
                              AppCompatActivity appCompatActivity) {
        this.bind = bind;
        this.getVerifyCode = getVerifyCode;
        this.activity = appCompatActivity;
    }

    public void setView(BindPhoneView view) {
        this.bindView = view;
    }

    public void getVerifyCode(String phone) {
        getVerifyCode.execute(new GetVerifyCodeSubscriber(activity), new Action0() {
            @Override
            public void call() {
                showCountDown = Observable.interval(0, 1000, TimeUnit.MILLISECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .onBackpressureDrop()
                        .subscribe(new BaseOnNext<Long>() {
                            @Override
                            public void call(Long aLong) {
                                if (aLong <= VERIFY_INTERVAL) {
                                    bindView.setGetVerifyCodeBtnStatus(false);
                                    bindView.countDown(VERIFY_INTERVAL - aLong);
                                } else {
                                    bindView.setGetVerifyCodeBtnStatus(true);
                                    showCountDown.unsubscribe();
                                }
                            }
                        });
            }
        }, new GetVerifyCodeTask(phone));
    }

    public void bind(String phone, String token, String verifyCode) {
        bind.execute(new RegisterSubscriber(activity), new BindPhoneTask(phone, token, verifyCode));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        bind.unSubscribe();
        getVerifyCode.unSubscribe();
        if (showCountDown != null) {
            showCountDown.unsubscribe();
        }
    }

    private class RegisterSubscriber extends BaseSubscriber<Boolean> {

        RegisterSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Boolean result) {
            super.onNext(result);
            if (result) {
                bindView.bindSuccess();
            } else {
                bindView.bindFail();
            }
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.bind_failed);
        }
    }

    private class GetVerifyCodeSubscriber extends BaseSubscriber<String> {

        GetVerifyCodeSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(String s) {
            super.onNext(s);
            bindView.setVerifyCode(s);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_verify_code_fail);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();

            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }
    }
}
